package com.wd.live.module_live.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wd.live.module_base.net.bean.live.CloseLiveBean
import com.wd.live.module_base.net.bean.live.FindFansInteractListBean
import com.wd.live.module_base.net.bean.live.LiveData
import com.wd.live.module_base.net.bean.live.OpenLiveBean
import com.wd.live.module_base.net.bean.live.ResultBean
import com.wd.live.module_base.net.util.HttpUtils2
import com.wd.live.module_base.ui.base.BaseResult
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.http.Query
import java.io.File

/**
 * 作者: 王晶晶
 * 时间:2024/5/31
 * 描述:
 */
class LiveViewModel:ViewModel() {
    var liveId = 0
    var agoraToken = ""
    var channelName = ""
    var compositeWsUrl = ""

    //开直播
    //    liveCoverImage	file	直播封面照	文件流	是
    var dataOpenLive=MutableLiveData<OpenLiveBean<ResultBean>>()
    fun postOpenLive(liveTitle:String, file:File){
        val body = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), file)
        val part: MultipartBody.Part = MultipartBody.Part.createFormData("liveCoverImage", file.name, body)
        viewModelScope.launch (Dispatchers.IO){
            val result=HttpUtils2.apiService.postOpenLive(101,liveTitle,"116.4074","39.3042",part)
            dataOpenLive.postValue(result)
        }
    }

    //推流
    var dataStartPushStream=MutableLiveData<BaseResult<String?>>()
    fun putStartPushStream(liveId:Int){
         viewModelScope.launch (Dispatchers.IO){
            val result=HttpUtils2.apiService.putStartPushStream(liveId)
            dataStartPushStream.postValue(result)
        }
    }
    //关直播
    var dataCloseLive=MutableLiveData<BaseResult<CloseLiveBean>>()
    fun deleteCloseLive(){
        viewModelScope.launch (Dispatchers.IO){
            val result=HttpUtils2.apiService.deleteCloseLive()
            dataCloseLive.postValue(result)
        }
    }


    var dataFindFansInteractList=MutableLiveData<BaseResult<List<FindFansInteractListBean>>>()
    fun getFindFansInteractList(liveId:Int){
        viewModelScope.launch (Dispatchers.IO){
            val result=HttpUtils2.apiService.getFindFansInteractList(liveId)
            dataFindFansInteractList.postValue(result)
        }
    }
    var dataPutPassFansInteract=MutableLiveData<BaseResult<String?>>()
    fun getPassFansInteract(liveId:Int, fansId:Int){
        viewModelScope.launch (Dispatchers.IO){
            val result=HttpUtils2.apiService.putPassFansInteract(liveId, fansId)
            dataPutPassFansInteract.postValue(result)
        }
    }

}